<template>
  <el-container>
    <el-container>
      <el-main>
        <el-card class="box-card">
          <el-button
            type="primary"
            @click="add"
            size="small"
            icon="el-icon-plus"
            >添加顶级部门</el-button
          >
        </el-card>
        <el-card class="box-card" style="margin-top: 10px">
          <el-table
            :data="tableData"
            style="width: 100%; margin-bottom: 20px"
            row-key="id"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          >
            <el-table-column prop="id" label="ID" width="180">
            </el-table-column>
            <el-table-column prop="name" label="部门名称" width="180">
            </el-table-column>
            <el-table-column label="状态" prop="status">
              <template #default="scope">
                <span v-if="scope.row.status == 1">
                  <el-tag size="medium" type="success">启用</el-tag>
                </span>
                <span v-else
                  ><el-tag size="medium" type="danger">禁用</el-tag></span
                >
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="创建时间">
            </el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="table_add(scope.row, scope.$index)"
                  v-if="scope.row.pid == 0"
                  icon="el-icon-plus"
                  >添加子部门</el-button
                >
                <el-divider direction="vertical"></el-divider>
                <el-button
                  type="text"
                  size="small"
                  @click="table_edit(scope.row, scope.$index)"
                  icon="el-icon-edit"
                  >编辑</el-button
                >
                <el-divider direction="vertical"></el-divider>
                <el-popconfirm
                  title="确定要删除吗？"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  @confirm="table_del(scope.row, scope.$index)"
                >
                  <template #reference>
                    <el-button type="text" size="small" icon="el-icon-delete"
                      >删除</el-button
                    >
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-main>
    </el-container>

    <save-dialog
      v-if="dialog.save"
      ref="saveDialog"
      @success="handleSaveSuccess"
      @closed="dialog.save = false"
    ></save-dialog>
  </el-container>
</template>

<script>
import saveDialog from "./save";

export default {
  name: "dept",
  components: {
    saveDialog,
  },
  data() {
    return {
      dialog: {
        save: false,
      },
      search: {
        name: "",
      },
      tableData: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    table_add(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add").setData({
          pid: row.id,
          status: 1,
        });
      });
    },
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    async table_del(row) {
      let res = await this.$api.dept.del.get({ id: row.id });
      if (res.code == 0) {
        this.$message.success(res.msg);
        this.getList();
      } else {
        this.$message.error(res.msg);
      }
    },
    async getList() {
      let res = await this.$api.dept.list.get();
      if (res.code == 0) {
        this.tableData = res.data;
      } else {
        this.$message.error(res.msg);
      }
    },
    handleSaveSuccess() {
      this.getList();
    },
  },
};
</script>

<style>
.list-title {
  display: inline-block;
  color: #17233d;
  font-weight: 500;
  font-size: 20px;
  border-left: 5px solid #409eff;
  padding-left: 20px;
}
</style>